package com.dl.service.impl;

import com.dl.dao.UserMapper;
import com.dl.pojo.Page;
import com.dl.pojo.Permission;
import com.dl.pojo.Role;
import com.dl.pojo.User;
import com.dl.service.UserService;
import com.dl.utils.ConstantField;
import com.dl.utils.SaltUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author DL
 * @create 2021-03-16 14:34
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public boolean register(User user) {
        //1.生成随机盐
        String salt = SaltUtils.getSalt(8);
        //2.将随机盐保存到数据库
        user.setSalt(salt);
        //3.明文密码进行md5 + salt + hash散列
        Md5Hash md5Hash = new Md5Hash(user.getPassword(),salt,1024);
        user.setPassword(md5Hash.toHex());
        return userMapper.addUser(user) == 1;
    }

    @Override
    public HashMap<String, String> modify(String originalPsd, String newPsd, String confirmNewPsd) {
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();//获取用户名
        User user = userMapper.queryUserByName(username);//通过用户名获取用户
        String DBPassword = user.getPassword();//获取数据库保存的密码
        HashMap<String, String> map = new HashMap<>();
        if (newPsd.equals(confirmNewPsd)){//判断两次密码是否一致
            //获取数据库存储的盐，进行加密验证
            String salt = user.getSalt();
            Md5Hash md5Hash = new Md5Hash(originalPsd,salt,1024);
            originalPsd = md5Hash.toHex();//加密原密码
            //判断原密码是否正确
            if (DBPassword.equals(originalPsd)){
                //加密新密码
                Md5Hash md5HashNewPsd = new Md5Hash(newPsd,salt,1024);
                user.setPassword(md5HashNewPsd.toHex());
                System.out.println("user.toString() = " + user.toString());
                if (userMapper.updateUser(user) == 1){
                    subject.getSession().setAttribute(ConstantField.LOGIN_INFO,user);
                    map.put("msg","修改成功");
                }else {
                    map.put("msg","修改失败");
                }
            }else{
                map.put("msg","原密码错误");
            }
        }else {
            map.put("msg","密码不一致");
        }
        System.out.println("map = " + map);
        return map;
    }

    @Override
    @Transactional(readOnly = true)
    public int getTotal() {
        return userMapper.getTotal();
    }

    @Override
    @Transactional(readOnly = true)
    public int getTotalByKey(String keyWord) {
        return userMapper.getTotalByKey(keyWord);
    }

    @Override
    public boolean addUser(User user) {
        return userMapper.addUser(user) == 1;
    }

    @Override
    public boolean deleteUser(int id) {
        return userMapper.deleteUser(id) == 1;
    }

    @Override
    public boolean updateUser(User user) {
        return userMapper.updateUser(user) == 1;
    }

    @Override
    public boolean updateUserInfo(Map<String, String> map) {
        return userMapper.updateUserInfo(map) == 1;
    }

    @Override
    public boolean updateStatusByID(int id, int status) {
        return userMapper.updateStatusByID(id, status) == 1;
    }

    @Override
    @Transactional(readOnly = true)
    public User queryUserByName(String username) {
        return userMapper.queryUserByName(username);
    }

    @Override
    @Transactional(readOnly = true)
    public User queryUserById(int id) {
        return userMapper.queryUserById(id);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Role> findRolesByUsername(String username) {
        return userMapper.findRolesByUsername(username);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Permission> findPermissionsByUsername(String username) {
        return userMapper.findPermissionsByUsername(username);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Permission> findPermissionsByRolesID(String roleID) {
        return userMapper.findPermissionsByRolesID(roleID);
    }

    @Override
    @Transactional(readOnly = true)
    public List<User> queryAllUser() {
        return userMapper.queryAllUser();
    }


    @Override
    @Transactional(readOnly = true)
    public Page<User> queryUserByLimit(int currentPage, int pageSize) {
        //map集合，保存分页基本信息
        Map<String, Integer> map = new HashMap<>();
        //分页对象，保存分页所有信息
        Page<User> page = new Page<>();
        //设置数据总数
        page.setTotal(userMapper.getTotal());
        //设置页面大小，同时计算页面总数
        page.setPageSize(pageSize);
        map.put("currentPage", (currentPage-1)*pageSize);
        map.put("pageSize", pageSize);

        //查询到的学生集合放入page对象中
        List<User> users = userMapper.queryUserByLimit(map);
        page.setList(users);
        return page;
    }

    @Override
    @Transactional(readOnly = true)
    public Page<User> queryUserByKey(String keyWord, int currentPage, int pageSize) {
        //map集合，保存分页基本信息
        Map<String, Object> map = new HashMap<>();
        //分页对象，保存分页所有信息
        Page<User> page = new Page<>();

        int totalByKey = userMapper.getTotalByKey(keyWord);
        if (totalByKey != 0){
            //设置数据总数
            page.setTotal(totalByKey);
            //设置页面大小，同时计算页面总数
            page.setPageSize(pageSize);

            map.put("keyWord", keyWord);
            map.put("currentPage", (currentPage-1)*pageSize);
            map.put("pageSize", pageSize);

            //查询到的学生集合放入page对象中
            List<User> users = userMapper.queryUserByKey(map);
            page.setList(users);
        }else {
            page = null;
        }
        return page;
    }
}
